﻿/*
 * @Autor: PK
 * @Date: 2021/12/26 21:07:SS
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using ShkSimulation.core.optimization.extractor;

namespace ShkSimulation.desktop.component.tools {

	public class TargetValueRowDisplay: IFormattable {

		public const string UniqueKeyName = "UniqueKey";

		private static int index = 1;
		
		public TargetValueRowDisplay(string uniqueKey, CellPoint cellPoint) {
			this.Index = index++;
			this.UniqueKey = uniqueKey;
			this.RowIndex = cellPoint.Row;
			this.CurrentValue = cellPoint.CurrentValue;
		}

		public int Index { get; private set; }
		public string UniqueKey { get; private set; }
		public int RowIndex { get; private set; }
		public string CurrentValue { get; private set; }

		public string ToString(string format, IFormatProvider formatProvider) {
			if (UniqueKeyName.Equals(format)) {
				return UniqueKey;
			}
			return ToString();
		}

	}

	public class TargetValueRowBindingList : BindingList<TargetValueRowDisplay> {

		public Dictionary<CellPoint, double> ResultRows { get; set; }
		
		public TargetValueRowBindingList(DataGridView reporterSelectView, Dictionary<CellPoint, double> rows = null) {
			if (rows != null && rows.Count != 0) {
				foreach (var row in rows) {
					Add(new TargetValueRowDisplay(row.Key.Name, row.Key));
				}
				this.ResultRows = new Dictionary<CellPoint, double>(rows);
			} else {
				this.ResultRows = new Dictionary<CellPoint, double>();
			}
			this.ReporterSelectView = reporterSelectView;
			ReporterSelectView.ResetBindings();
			reporterSelectView.AutoGenerateColumns = true;
			ReporterSelectView.DataSource = this;
		}

		public Dictionary<string, double> GetValueMap() {
			return this.ToDictionary(each => each.UniqueKey, each => Convert.ToDouble(each.CurrentValue));
		}

		private DataGridView ReporterSelectView { get; set; }

	}
}